<?php include_once("connection.php"); // the connection to the database ?>
<div style="overflow:hidden; margin:1em;">
<h1>Update</h1>
<?php if($_GET['submitted']) { 
	
	$newCategory = mysql_query("UPDATE category SET category_label = '".$_POST['category_name']."' WHERE category_id = ".$_POST['category_id']) or die(mysql_error());
	?>
	<script type="text/javascript">	
		$(document).ready(function(){
			$.nyroModalRemove();
		});
	</script>	
	<?php
	
} else { 

// extract the max order number: the new category will have this order number + 1
$rsCategoryName = mysql_query("SELECT category_label FROM category WHERE category_id = ".$_GET['category_id']) or die(mysql_error());
$results = mysql_fetch_array($rsCategoryName);
$categoryName = $results[0];

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]."?submitted=true" ?>" name="modify" id="modify" class="nyroModal">
	<label for="category_name"></label>
	<input type="text" value="<?php echo $categoryName ?>" name="category_name" id="category_name" />
	<input type="hidden" value="<?php echo $_GET['category_id'] ?>" name="category_id" id="category_id" />
	<input type="submit" value="Modify" id="form_submit" />
</form>
<script type="text/javascript">	
	$(document).ready(function(){
		$("#form_submit").click(function(e){
			e.preventDefault();
			if ( $("#category_name").val() != "" ) $("#modify").submit();
			else alert("Please insert the name of the new category");
		});
	});
</script>	 
<?php } ?>
</div>